/*
 * 文 件 名:  MyBatisConfig.java
 * 版    权:  武汉虹信通信技术有限责任公司. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  LENOVO
 * 修改时间:  2017年9月25日
 * 修改内容:  <修改内容>
 */
package com.activiti.config;

import java.beans.PropertyVetoException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.github.pagehelper.PageHelper;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 本地数据源配置
 * @author 0380009081
 *
 */
@Configuration
@PropertySource("classpath:com/activiti/config/dbConfig.properties")
public class MybatisConfiguration {

	@Value("${db.jdbc.driver}")
	private String driver;
	@Value("${db.jdbc.url}")
	private String url;
	@Value("${db.jdbc.username}")
	private String username;
	@Value("${db.jdbc.password}")
	private String password;
	@Value("${db.jdbc.maxIdel}")
	private int maxIdel;

	@Bean(name = "dbDataSource")
	@Primary
	public DataSource dataSource() {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		try {
			dataSource.setDriverClass(driver);
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
		dataSource.setMaxPoolSize(3);
		dataSource.setInitialPoolSize(1);
		dataSource.setJdbcUrl(url);
		dataSource.setUser(username);
		dataSource.setPassword(password);
		dataSource.setMaxIdleTime(maxIdel);
		return dataSource;
	}

	@Bean("dbSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dbDataSource") DataSource dataSource) {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("reasonable", "true");
		properties.setProperty("supportMethodsArguments", "true");
		properties.setProperty("returnPageInfo", "check");
		properties.setProperty("params", "count=countSql");
		pageHelper.setProperties(properties);

		// 添加插件
		bean.setPlugins(new Interceptor[] { pageHelper });

		// 添加XML目录
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			bean.setMapperLocations(resolver.getResources("classpath*:com/activiti/mapping/*.xml"));
			return bean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Bean("dbSqlSessionTemplate")
	public SqlSessionTemplate sqlSessionTemplate(
			@Qualifier("dbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	@Bean("dbPlatformTransactionManager")
	public PlatformTransactionManager annotationDrivenTransactionManager(
			@Qualifier("dbDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

}
